package org.example.myleet.learn;

import java.util.Arrays;

public class LotteryDrawing {

    /**
     * 挑出K个
     */
    private static final int K = 50;

    /**
     * 总数字1～N
     */
    private static final int N = 100;

    /**
     * 无重复地从N个数中随机抽取K个数
     */
    public static void main(String[] args) {
        int[] numbers = new int[N];
        for (int i = 1; i <= N; ++i) {
            numbers[i - 1] = i;
        }
        int[] result = new int[K];
        int n = N;
        for (int i = 0; i < K; ++i) {
            //随机在剩下的数字中抽取一个
            int r = (int) (Math.random() * n);
            result[i] = numbers[r];
            //关键点：将抽过的数字的位置放入当前需要抽取的最后一个数，并缩小n，巧妙地利用很低的计算复杂度避开了重复抽取的问题
            numbers[r] = numbers[n - 1];
            --n;
        }
        Arrays.sort(result);
        System.out.println(Arrays.toString(result));
    }
}
